<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="StyleSheet" type="text/css" href="{%$templates%}/admin.css">
<link type="text/css" rel="stylesheet" href="{%$library%}/yui/fonts.css">
<link type="text/css" rel="stylesheet" href="{%$library%}/yui/datatable.css" />

<style type="text/css">
#tt ul {margin:0;padding:0;list-style-type:none;width:auto;}
#tt li {display:block;float:none;margin:2px 0px;padding:2px 20px;}
.grid {margin:0px 10px;}
</style>

<script type="text/javascript" src="{%$library%}/yui/utilities.js"></script>
<script type="text/javascript" src="{%$library%}/yui/datasource.js"></script>
<script type="text/javascript" src="{%$library%}/yui/datatable.js"></script>
<script type="text/javascript" src="{%$library%}/yui/element.js"></script>
<script type="text/javascript" src="{%$library%}/yui/connection.js"></script>

<script language="javascript" type="text/javascript">

var oD;
var Point;
//YAHOO.util.Event.addListener(window, "load", LoadGrid);
function LoadGrid(category)
{
   YAHOO.cf.XHR_Text = new function() 
   {
      YAHOO.widget.DataTable.MSG_ERROR = 'У данного модуля конфигурационные переменные отсутствуют';

      this.editValue = function(oEditor, oSelf)
      {
         var elCell = oEditor.cell;
         var oRecord = oEditor.record;
         var oColumn = oEditor.column;
         var elContainer = oEditor.container;
         var value = oRecord.getData(oColumn.key);
         var elTextbox = elContainer.appendChild(document.createElement("input"));
         elTextbox.type = "text";
         elTextbox.style.width = (elCell.offsetWidth + 20) + "px";
         elTextbox.value = value;

         YAHOO.util.Event.addListener(elTextbox, "keyup", function(v)
         {
            // Save on "enter"
            if(v.keyCode === 13)
            {
               oSelf.saveCellEditor();
            } 
            // Update the tracker value
            else
            {
               oSelf._oCellEditor.value = elTextbox.value;
            }
         });
         // Select the text 
         elTextbox.select();
      };

      var myColumnDefs =
      [
         {key:"Переменная", sortable:true, resizeable:true},
         {key:"Тип", sortable:true, resizeable:true},
         {key:"Значение", editor:this.editValue, resizeable:true},
         {key:"Описание", sortable:false, resizeable:true}
      ];

      this.myDataSource = new YAHOO.util.DataSource("config.php?a=1&id="+category);
      this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_TEXT;
      this.myDataSource.responseSchema =
      {
         recordDelim: "^^",
         fieldDelim: "##",
         fields: ["Переменная","Тип","Значение","Описание"]
      };

      this.myDataTable = new YAHOO.widget.DataTable("grid_"+category, myColumnDefs, this.myDataSource);

      // Set up editing flow
      this.highlightEditableCell = function(oArgs)
      {
         var elCell = oArgs.target;
         if(YAHOO.util.Dom.hasClass(elCell, "yui-dt-editable"))
         {
            this.highlightCell(elCell);
         }
      };
      this.myDataTable.subscribe("cellMouseoverEvent", this.highlightEditableCell);
      this.myDataTable.subscribe("cellMouseoutEvent", this.myDataTable.onEventUnhighlightCell);
      this.myDataTable.subscribe("cellClickEvent", this.myDataTable.onEventShowCellEditor);

      // Hook into custom event to customize save-flow of "radio" editor
      this.myDataTable.subscribe("editorUpdateEvent", function(oArgs)
		{
         if(oArgs.editor.column.key === "active")
			{
            this.saveCellEditor();
         }
      });

      this.myDataTable.subscribe("editorBlurEvent", function(oArgs)
		{
         this.cancelCellEditor();
      });

		function handleSuccess(o)
		{
			if(o.responseText !== undefined)
			{
				if(o.responseText != 1) handleFailure(o);
			}
		}

		function handleFailure(o)
		{
			Point.cell.innerHTML = oD;
		}

		var callback =
		{
			success:handleSuccess,
			failure:handleFailure,
			argument:['foo','bar']
		};

		this.myDataTable.subscribe("editorSaveEvent", function(oArgs)
		{
			oD = oArgs.oldData;
			Point = oArgs.editor;
			var nD = oArgs.newData;
			var vD = Point.record.getData("Переменная");
			var sUrl = "config.php";
			var postData = "a=2&newData="+nD+"&Vname="+vD;
			var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
		});
   };
}

function showGr(el, category)
{
   el.setAttribute("onclick", "hideGr(this, "+category+")");
   LoadGrid(category);
}

function hideGr(el, category)
{
   el.setAttribute("onclick", "showGr(this, "+category+")");
   document.getElementById("grid_"+category).innerHTML = "";
}

</script>
</head>
<body bgcolor='#ffffff' marginheight='0' topmargin='0'>

<table border='0' cellpadding='0' cellspacing='1' width='100%'>
<tr>
<td width='160px' style='border-right: solid 1px #ffcc44' valign='top'>

{%include file='admin.meccano-nav.tpl.html'%}

</td>
<td align='left' valign='top'>

<div align="center" style="padding:10px" class="title darkgrey">
	Конфигурационные переменные системы:
</div>

<div id="outputDiv"></div>

{%foreach from=$modules item="value" key="mod" name=i%}
{%if $smarty.foreach.i.first%}
<ul id="tt">
<li><span class="ln" onclick="showGr(this, 0)"><b>Общесистемные</b></span></li>
<div id="grid_0" class="grid"></div>
{%/if%} 
<li><span class="ln" onclick="showGr(this, {%$mod%})"><b>{%$value.title%}</b></span></li>
<div id="grid_{%$mod%}" class="grid"></div>
{%if $smarty.foreach.i.last%}</ul>{%/if%} 
{%/foreach%}

</td>
</tr>
</table>
<div id='test' style="padding:20px"></div>
</body>
</html>